import { Component, OnInit, ViewChild } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { DatePipe } from '@angular/common';
import { ReuseTabService } from '@delon/abc';
import { ActivatedRoute, Params } from '@angular/router';
import { environment } from '../../../../environments/environment';
import { TableCheckPageComponent } from '../../../layout/components/tableCheckPage.component';
import { ExportFileService } from '../../../core/exportFile';

@Component({
    selector: 'app-securityproblem',
    templateUrl: './securityProblem.component.html',
    styleUrls: ['./securityProblem.component.less']
})

export class SecurityProblemComponent implements OnInit {
    @ViewChild('tcp', { static: true }) tcp: TableCheckPageComponent;
    fold = { 'text': '展开', 'icon': 'down', 'status': true };
    btn = { 'text': '展开', 'icon': 'down', 'status': true };
    searchParam: any = {};
    apiUrl: any = {};
    tableScroll: any = {};
    exportUrl = '';
    headData = [];
    plainData = [];
    tempData = [];

    operaList: any = [
        { addModal: false },
        { editModal: false },
        { delModal: false },
        { tbModal: false },
        { xfModal: false },

        { qsModal: false },
        { pzModal: false },
        { bhModal: false },
        { sqModal: false },
        { dyhkModal: false },

        { zgwtModal: false },
        { sqyqModal: false },
        { tyyqModal: false },
        { bhyqModal: false },
        { detailModal: false },

        { zyhkModal: false },
        { aqktModal: false },
        { aqkpzModal: false },
        { aqkbhModal: false }
    ];

    jcbmSelect = [];
    zrbmSelect = [];
    wtlbSelect = [];
    wtflSelect = [];
    wtdjSelect = [];
    sfxhSelect = [];
    gzxmSelect = [];
    jcddAutoData = [];
    jcddTempData = [];
    paramType = '';
    paramReadOnly = '';
    paramOver = '';
    tabIndex = 0;
    treeNodeStyle = { 'max-height': '300px', 'overflow-y': 'auto', 'overflow-x': 'hidden' };

    constructor(
        private http: HttpClient,
        private activatedRoute: ActivatedRoute,
        private exportFile: ExportFileService,
        private reuseTabService: ReuseTabService,
        private datePipe: DatePipe
    ) { }

    ngOnInit() {
        this.activatedRoute.params.subscribe((params: Params) => {
            this.paramType = params.status;
            switch (params.status) {
                case 'edit':
                    this.paramReadOnly = '&readOnly=0';
                    this.reuseTabService.title = '安全问题库';
                    break;
                case 'view':
                    this.paramReadOnly = '&readOnly=1';
                    this.reuseTabService.title = '问题库总表';
                    break;
                case 'add':
                    this.modalShow('addModal');
                    break;
            }
            this.headData = [
                { "name": "状态", "width": "105px" },
                { "name": "工作项目", "width": "8%" },
                { "name": "检查部门", "width": "6%" },
                { "name": "检查时间", "width": "5%" },
                { "name": "检查地点", "width": "4%" },

                { "name": "检查人", "width": "3.5%" },
                { "name": "责任部门", "width": "6%" },
                { "name": "问题描述", "width": "10%" },
                { "name": "整改要求" },
                { "name": "整改期限", "width": "5%" },

                { "name": "销号部门", "width": "6%" },
                { "name": "问题类别", "width": "5%" },
                { "name": "问题分类", "width": "5%" },
                { "name": "问题等级", "width": "5%" },
                { "name": "设备类型", "width": "6%" },

                { "name": "备注", "width": "5%" }
            ];
            this.plainData = [
                "cancledStatus", "projectName", "checkOrgName", "checkDate", "checkPlace",
                "checkPerson", "chargeOrgName", "problemDesc", "handleRequire", "deadline",
                "cancleOrgName", "typeName", "problemClass", "problemLevel", "devTypeName",
                "remark"
            ];
            this.tableScroll.x = '2000px';
            this.http.get(environment.apiBase + '/csmis/getKmnOrgTree.jwt?showAll=1&showSeg=1').subscribe((res: any) => {
                Object.assign(this.jcbmSelect, res);
                Object.assign(this.zrbmSelect, res);
            });
            this.http.get(environment.apiBase + '/csmis/publicTree/getorgstatree.jwt').subscribe((res: any) => {
                this.autoToList(this.jcddTempData, res);
            });
            this.http.get(environment.project + '/sysTDict/getDictByClass?code=SAFE_PROBLEM_TYPE').subscribe((res: any) => {
                if (!res.code) {
                    this.wtlbSelect = res.data;
                }
            });
            this.http.get(environment.project + '/sysTDict/getDictByClass?code=PROBLEM_CLASS').subscribe((res: any) => {
                if (!res.code) {
                    this.wtflSelect = res.data;
                }
            });
            this.http.get(environment.project + '/sysTDict/getDictByClass?code=PROBLEM_LEVEL').subscribe((res: any) => {
                if (!res.code) {
                    this.wtdjSelect = res.data;
                }
            });
            this.http.get(environment.project + '/sysTDict/getDictByClass?code=SAFE_PROBLEM_PROJECT').subscribe((res: any) => {
                if (!res.code) {
                    this.gzxmSelect = res.data;
                }
            });
            // this.lbSelect = [
            //     { value: '0', name: '发起问题' },
            //     { value: '1', name: '已下发' },
            //     { value: '2', name: '已签收' },
            //     { value: '3', name: '已上报' },
            //     { value: '4', name: '申请延期' },
            //     { value: '5', name: '延期同意' },
            //     { value: '6', name: '延期驳回' },
            //     { value: '7', name: '申请销号' },
            //     { value: '8', name: '销号同意' },
            //     { value: '9', name: '销号驳回' },
            //     { value: '10', name: '逾期销号' }
            // ];
            this.sfxhSelect = [
                { value: '1', name: '是' },
                { value: '0', name: '否' },
                { value: '2', name: '超期' }
            ];
            this.apiUrl.add = environment.project + '/safeProblem/add';
            this.apiUrl.edit = environment.project + '/safeProblem/update';
            this.apiUrl.del = environment.project + '/safeProblem/delete';
            this.apiUrl.tb = environment.project + '/safeProblem/submit';
            this.apiUrl.xf = environment.project + '/safeProblem/send';

            this.apiUrl.qs = environment.project + '/safeProblem/accept';
            this.apiUrl.pz = environment.project + '/safeProblem/approveCancle';
            this.apiUrl.bh = environment.project + '/safeProblem/rejectCancle';
            this.apiUrl.sqxh = environment.project + '/safeProblem/applyCancle';
            this.apiUrl.wcrw = environment.project + '/safeProblem/finish';
            this.apiUrl.zgwt = environment.project + '/safeProblem/handle';

            this.apiUrl.sqyq = environment.project + '/safeProblem/applyDelay';
            this.apiUrl.tyyq = environment.project + '/safeProblem/approveDelay';
            this.apiUrl.bhyq = environment.project + '/safeProblem/rejectDelay';
            this.apiUrl.zyhk = environment.project + '/safeProblem/toHideProblem';

            this.apiUrl.aqktb = environment.project + '/safeProblem/';
            this.apiUrl.aqkpz = environment.project + '/safeProblem/';
            this.apiUrl.aqkbh = environment.project + '/safeProblem/';
            this.loadList();
        });
    }
    loadList() {
        this.tcp.loadList(environment.project + '/safeProblem/query', this.paramReadOnly + this.paramOver);
    }
    tabsChange(e) {
        switch (e.index) {
            case 0:
                this.paramOver = '';
                break;
            case 1:
                this.paramOver = '&over=0';
                break;
            case 2:
                this.paramOver = '&over=1';
                break;
        }
        this.loadList();
    }
    jcddInput(e) {
        const value = e.target.value;
        this.jcddAutoData = value ? this.jcddTempData.filter(option => option.indexOf(value) !== -1) : [];
    }
    // children转list
    autoToList(array, list) {
        list.forEach(item => {
            array.push(item.text);
            if (item.children && item.children.length) {
                this.autoToList(array, item.children);
            }
        })
    }
    foldClick() {
        if (this.fold.status) {
            this.fold.text = '收起';
            this.fold.icon = 'up';
        } else {
            this.fold.text = '展开';
            this.fold.icon = 'down';
        }
        this.tcp.clickFold(this.fold.status, 1);
        this.fold.status = !this.fold.status;
    }
    search() {
        let params = '';
        for (const item of Object.keys(this.searchParam)) {
            const data = this.searchParam[item];
            if (data || data === 0) {
                params += '&' + item + '=';
                if (data instanceof Date) {
                    params += this.datePipe.transform(data, 'yyyy-MM-dd');
                } else {
                    params += data;
                }
            }
        }
        params += this.paramReadOnly + this.paramOver;
        this.exportUrl = params;
        this.tcp.loadList(environment.project + '/safeProblem/query', params);
    }
    export() {
        this.exportFile.getDown(environment.project + '/safeProblem/export?page=1&limit=20' + this.paramReadOnly + this.exportUrl, '安全问题库.xls');
    }
    modalShow(type) {
        this.tempData = this.tcp.getAllData();
        this.operaList[type] = true;
    }
    modalHide(type) {
        this.operaList[type] = false;
    }
}
